home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 25 user and custom controls / customcontrollibrary / comboboxex.vb < prev    next >
Encoding:
Text File  |  2002-03-19  |  5.9 KB  |  161 lines

  1. ' This class is a ComboBoxEx control, that implements a 
  2. ' control that is similar to the standard DropDownList control, with a 
  3. ' the following enhancements: you can assign all elements in one operation 
  4. ' as a comma-separated list to the ItemList property
  5.  
  6. ' The real purpose of this sample is to show how you can implement
  7. ' features that most standard controls have, such as support for
  8. ' the AutoPostBack property and exposing of a postback event.
  9.  
  10. Imports System.ComponentModel
  11. Imports System.Web.UI
  12.  
  13. <ToolboxData("<{0}:ComboBoxEx runat=server></{0}:ComboBoxEx>")> Public Class ComboBoxEx
  14.     Inherits System.Web.UI.WebControls.WebControl
  15.     Implements IPostBackDataHandler
  16.     Implements IPostBackEventHandler
  17.  
  18.     ' These event definitions are equivalent.
  19.     'Event SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
  20.     Event SelectedIndexChanged As EventHandler
  21.  
  22.     ' The ItemList property
  23.     ' Format is "option,value&option,value&...."
  24.     Dim m_ItemList As String = "First,Second,Third"
  25.  
  26.     Property ItemList() As String
  27.         Get
  28.             Return m_ItemList
  29.         End Get
  30.         Set(ByVal Value As String)
  31.             m_ItemList = Value
  32.         End Set
  33.     End Property
  34.  
  35.     ' The SelectedIndex property
  36.     <DefaultValue(0)> _
  37.     Property SelectedIndex() As Integer
  38.         Get
  39.             ' Notice how you can avoid to query ViewState twice
  40.             Dim o As Object = Me.ViewState("SelectedIndex")
  41.             If o Is Nothing Then
  42.                 Return 0   ' the default value
  43.             Else
  44.                 Return CInt(o)
  45.             End If
  46.         End Get
  47.         Set(ByVal Value As Integer)
  48.             If Value >= 0 Then
  49.                 ' assign a new value only if it's a valid value
  50.                 Me.ViewState("SelectedIndex") = Value
  51.             End If
  52.         End Set
  53.     End Property
  54.  
  55.     ' The AutoPostBack property
  56.  
  57.     Property AutoPostBack() As Boolean
  58.         Get
  59.             Dim o As Object = Me.ViewState("AutoPostBack")
  60.             If o Is Nothing Then
  61.                 Return False
  62.             Else
  63.                 Return CBool(o)
  64.             End If
  65.         End Get
  66.         Set(ByVal Value As Boolean)
  67.             Me.ViewState("AutoPostBack") = Value
  68.         End Set
  69.     End Property
  70.  
  71.     ' this is the method that overrides the default behavior and
  72.     ' outputs the actual HTML code
  73.  
  74.     Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
  75.         ' get the array from the ItemList property
  76.         Dim items() As String = ItemList.Split(","c)
  77.  
  78.         ' output the <select> tag and its attributes.
  79.         output.AddAttribute("name", Me.UniqueID)
  80.         Me.AddAttributesToRender(output)
  81.  
  82.         ' if this control requires auto postback
  83.         If AutoPostBack Then
  84.             ' add the two attributes that implement it.
  85.             ' Note the GetPostBackEventReference method will actually
  86.             ' output the javascript code for the __doPostBack routine.
  87.             output.AddAttribute("onchange", Page.GetPostBackEventReference(Me))
  88.             output.AddAttribute("language", "javascript")
  89.         End If
  90.  
  91.         ' next methods outputs the SELECT tag and all the attributes
  92.         ' defined previously
  93.         output.RenderBeginTag("select")
  94.  
  95.         ' now we can add the individual <option> tags
  96.         Dim index As Integer
  97.  
  98.         For index = 0 To items.Length - 1
  99.             ' Add the Value attribute.
  100.             output.AddAttribute("value", index.ToString)
  101.  
  102.             ' If this is the selected element, add a "selected" attributes
  103.             If index > 0 AndAlso index = SelectedIndex Then
  104.                 output.AddAttribute("selected", "selected")
  105.             End If
  106.  
  107.             ' Output the <option> tag (and all the attributes set previously)
  108.             output.RenderBeginTag("option")
  109.             output.Write(items(index))
  110.             output.RenderEndTag()
  111.         Next
  112.         ' close the </select> tag.
  113.         output.RenderEndTag()
  114.     End Sub
  115.  
  116.     ' this method is invoked when a postback on the parent form occurs
  117.  
  118.     Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData
  119.         ' The post value is equal to the index of the selected element.
  120.         Dim newSelectedIndex As Integer = CInt(postCollection(postDataKey))
  121.         If SelectedIndex <> newSelectedIndex Then
  122.             ' if the new value is different, store in the property.
  123.             SelectedIndex = newSelectedIndex
  124.             ' Tell ASP.NET to call the RaisePostDataChangedEvent method.
  125.             Return True
  126.         End If
  127.     End Function
  128.  
  129.     ' this method is invoked when all the other controls have processed
  130.     ' their LoadPostData method, and only if the LoadPostData method
  131.     ' for this ComboBoxEx control returned True.
  132.  
  133.     Public Sub RaisePostDataChangedEvent() Implements System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent
  134.         ' raise an event in the parent form
  135.         OnSelectedIndexChanged(EventArgs.Empty)
  136.     End Sub
  137.  
  138.     ' inherited controls can override this method to control if and when
  139.     ' they raise the SelectedIndexChanged event in their clients
  140.  
  141.     Protected Overridable Sub OnSelectedIndexChanged(ByVal e As EventArgs)
  142.         RaiseEvent SelectedIndexChanged(Me, e)
  143.     End Sub
  144.  
  145.     ' another public event
  146.     Public Event Click As EventHandler
  147.  
  148.     ' this method is invoked if this control caused a postback
  149.     Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
  150.         OnClick(EventArgs.Empty)
  151.     End Sub
  152.  
  153.     ' inherited controls can override this method to control if and when
  154.     ' they raise the Click event in their clients
  155.  
  156.     Protected Overridable Sub OnClick(ByVal e As EventArgs)
  157.         RaiseEvent Click(Me, e)
  158.     End Sub
  159.  
  160. End Class
  161.